package com.yiyuan.weixin.weixinInservice.format;

import com.yiyuan.weixin.model.Article;
import com.yiyuan.weixin.model.ReceiveXmlEntity;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2017/3/22.
 */
public class FormatXmlNews implements FormatXmlService {
    @Override
    public String formatXml(ReceiveXmlEntity receiveXmlEntity) {
       // initReceiveXmlEntity(receiveXmlEntity);
        StringBuffer sb = new StringBuffer();
        Date date = new Date();
        sb.append("<xml><ToUserName><![CDATA[");
        sb.append(receiveXmlEntity.getFromUserName());
        sb.append("]]></ToUserName><FromUserName><![CDATA[");
        sb.append(receiveXmlEntity.getToUserName());
        sb.append("]]></FromUserName><CreateTime>");
        sb.append(date.getTime());
        sb.append("</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>");
        sb.append(receiveXmlEntity.getArticleCount());
        sb.append("</ArticleCount><Articles>");
        sb.append(getArticleItem(receiveXmlEntity.getArticles()));
        sb.append("</Articles></xml>");
        return sb.toString();

    }




    private String getArticleItem(List<Article> list){
        StringBuffer sb = new StringBuffer();
        for (Article article : list) {
            sb.append("<item><Title><![CDATA[");
            sb.append(article.getTitle());
            sb.append("]]></Title><Description><![CDATA[");
            sb.append(article.getDescription());
            sb.append("]]></Description><PicUrl><![CDATA[");
            sb.append(article.getPicurl());
            sb.append("]]></PicUrl><Url><![CDATA[");
            sb.append(article.getUrl());
            sb.append("]]></Url></item>");
        }
        return sb.toString();
    }

}